package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.business.VaccinationSite;
import com.marktotop.entity.business.Vaccinator;
import com.marktotop.entity.system.Log;
import com.marktotop.error.SystemCodeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.mapper.VaccinationSiteMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IReservationRecordService;
import com.marktotop.service.IVaccinationSiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.service.IVaccineDevolutionService;
import com.marktotop.vo.business.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class VaccinationSiteServiceImpl extends ServiceImpl<VaccinationSiteMapper, VaccinationSite> implements IVaccinationSiteService {

    @Autowired
    private VaccinationSiteMapper siteMapper;

    @Autowired
    private IVaccineDevolutionService devolutionService;

    @Autowired
    private IReservationRecordService recordService;

    /**
     * 分页展示所有接种点信息，模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param site
     * @return
     */
    @Override
    public ResultBean findSiteList(Integer pageNum, Integer pageSize, VaccinationSite site) {
        // 开启分页
        Page<VaccinationSite> page = new Page<>(pageNum,pageSize);
        IPage<VaccinationSite> sitesByPage = siteMapper.getSitesByPage(page,site);
        if (sitesByPage.getRecords().isEmpty()){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取全部接种点信息成功！")
                .data("total",sitesByPage.getTotal())
                .data("siteList",sitesByPage.getRecords());
    }


    /**
     * 添加接种地点信息
     *
     * @param site
     * @return
     */
    @Override
    public boolean addVaccinationSite(VaccinationSite site){
        site.setCreateTime(new Date());
        if (siteMapper.insert(site) == 0){
            return false;
        }
        return true;
    }


    /**
     * 编辑接种地点信息
     *
     * @param id
     * @return
     */
    @Override
    public VaccinationSite editVaccinationSite(Long id) throws SystemException {
        VaccinationSite site = siteMapper.selectById(id);
        if(null == site){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该编辑的接种点信息不存在...");
        }
        return site;
    }


    /**
     * 获取所有接种点列表，供疫苗下发使用
     *
     * @return
     */
    @Override
    public List<VaccSitesVO> findVaccinationSitesList(){
        return siteMapper.getAllSitesForOther();
    }


    /**
     * 获取该接种地的所有疫苗信息
     *
     * @param id
     * @return
     */
    @Override
    public SiteAndVaccsVO getSiteVaccineList(Long id) {
        return siteMapper.selectSiteVaccines(id);
    }


    /**
     * 查询所有拥有某一疫苗的接种地点信息
     *
     * @param id
     * @return
     */
    @Override
    public VaccineAndSitesVO getAllSiteByVid(Long id) {
        return siteMapper.selectAllSiteByVid(id);
    }


    /**
     * 删除接种地点信息
     *      前提:接种地无进行的预约信息，并且没有关联的疫苗
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @Override
    public boolean removeVaccinationSite(Long id) throws SystemException {
        VaccinationSite vaccinationSite = siteMapper.selectById(id);
        if (null == vaccinationSite){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"没有找到相关接种点信息...");
        }
        // 判断该接种点是否有下发疫苗，如果有，查询所有疫苗库存是否都为0
        if (devolutionService.haveRecordsBySiteId(id)){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该接种点存在有疫苗信息，不能删除...");
        }
        // 判断该接种点是否有预约的记录
        SiteReservationVO recordsBySite = recordService.getRecordsBySite(id);
        if (recordsBySite != null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该接种点存在正在进行的预约信息，不能删除...");
        }
        // 可以删除
        if (0 == siteMapper.deleteById(id)){
            return false;
        }
        return true;
    }


}
